import { Injectable } from '@nestjs/common';
import { IPagination, PaginateReturn } from '../types';

@Injectable()
export class CommonService<T = any> {
  getGlobalValue() {
    return 'value';
  }
  pagination<U extends IPagination>(data: T[], options: U): PaginateReturn<T> {
    const total = data.length;
    const prev = Number(options.pageNumber);
    const next =
      options.pageSize * options.pageNumber < total
        ? Number(options.pageNumber) + 1
        : null;
    const start = (options.pageNumber - 1) * options.pageSize;
    const end = options.pageNumber * options.pageSize;
    return {
      paginateMeta: {
        total,
        prev,
        next,
      },
      items: data.slice(start, end),
    };
  }
}
